package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.mapper.AccountMapper;
import com.cskaoyan.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author stone
 * @date 2022/07/12 09:42
 */
@Controller
@RequestMapping("account")
public class AccountController {

    @Autowired
    AccountService accountService;
    /*@Autowired
    AccountMapper accountMapper;*/

    //localhost:8080/account/transfer?fromId=1&destId=2&money=1000
    @RequestMapping("transfer")
    @ResponseBody
    public BaseRespVo transfer(HttpServletRequest request) {
        int fromId = Integer.valueOf(request.getParameter("fromId"));
        int destId = Integer.valueOf(request.getParameter("destId"));
        int money = Integer.valueOf(request.getParameter("money"));
        //accountService.transfer(fromId, destId, money);
        // 使用accountMapper执行转账业务
        /*int fromMoney = accountService.selectMoneyById(fromId);
        int destMoney = accountMapper.selectMoneyById(destId);

        fromMoney -= money;
        destMoney += money;

        // 发生异常的情况，事务提交了
        accountService.updateMoneyById(fromId, fromMoney);
        // 制造异常
        int i = 1 / 0;
        accountMapper.updateMoneyById(destId, destMoney);*/
        return BaseRespVo.ok("zhuanzhang chenggong");
    }

    //localhost:8080/account/transfer2?fromId=1&destId=2&money=1000
    @RequestMapping("transfer2")
    @ResponseBody
    public BaseRespVo transfer(int fromId,int destId,int money) {
        accountService.transfer(fromId, destId, money);
        return BaseRespVo.ok("zhuanzhang chenggong");
    }

}
